﻿using System;
using System.Globalization;

namespace QueryBuilder.Core.Common
{
    public static class Validator
    {
        /// <summary>
        /// Raise <see cref="ArgumentException"/> if  value of type<see cref="T"/> is not Assignable from <see cref="value"/>
        /// </summary>
        /// <typeparam name="T">
        /// generic type
        /// </typeparam>
        /// <param name="value">
        /// assingable value
        /// </param>
        public static void AssignableFrom<T>(object value)
        {
            if (value != null)
            {
                if (!typeof (T).IsAssignableFrom(value.GetType()))
                {
                    throw new InvalidCastException(
                        string.Format(CultureInfo.CurrentCulture, value.GetType().FullName, typeof (T).FullName));
                }
            }
        }

        /// <summary>
        /// return true if value of type <see cref="T"/> is not assignable from <see cref="value"></see>
        /// </summary>
        /// <typeparam name="T">
        /// generic type
        /// </typeparam>
        /// <param name="value">
        /// assignable valu
        /// </param>
        /// <returns>
        /// returns true is value is assignable from type T
        /// </returns>
        public static bool IsAssignableFrom<T>(object value)
        {
            if (value == null)
            {
                return true;
            }

            return typeof (T).IsAssignableFrom(value.GetType());
        }

        /// <summary>
        /// Raise <see cref="ArgumentNullException"/> if <see cref="value"/> is null.
        /// </summary>
        /// <param name="value">
        /// value for check
        /// </param>
        public static void NotNull(object value)
        {
            NotNull(value, new ArgumentNullException("value"));
        }

        /// <summary>
        /// Raise <see cref="ArgumentNullException"/> if <see cref="value"/> is null.
        /// </summary>
        /// <param name="value">
        /// value for check
        /// </param>
        /// <param name="exception">
        /// throws exception
        /// </param>
        public static void NotNull(object value, Exception exception)
        {
            if (value == null)
            {
                throw exception;
            }
        }
    }
}